const pathSep = require('path').sep;
const useIndex = true; //是否使用递增的数字作为模块的id，如果为false则使用模块相对的路径名作为模块id
let curModuleId = -100; //基础包ModuleId
let curBuzModuleId = -100; //业务包ModuleId
let baseModuleIdMap = [];
let buzModuleIdMap = [];
const fs = require('fs');
var baseMappingPath;
var buzMappingPath;

/** 根据模块路径返回moduleId，优点是简单且确保唯一，缺点是无法使用rambundle打包方式*/
function getModuleIdByName(projectRootPath, path) {
  let name = '';
  if (
    path.indexOf(
      'node_modules' +
        pathSep +
        'react-native' +
        pathSep +
        'Libraries' +
        pathSep
    ) > 0
  ) {
    name = path.substr(path.lastIndexOf(pathSep) + 1);
  } else if (path.indexOf(projectRootPath) == 0) {
    name = path.substr(projectRootPath.length + 1);
  }
  name = name.replace(/\.[^.]+$/, ''); // 使用正则去除扩展名
  let regExp =
    pathSep == '\\' ? new RegExp('\\\\', 'gm') : new RegExp(pathSep, 'gm');
  name = name.replace(regExp, '_'); //把path中的/换成下划线
  if (name.includes('node_modules')) {
    name = name.replace(/^src_[^_]+_/, ''); //新增 过滤 src中的路径
  }

  return name;
}

function getModuleId(projectRootPath, path) {
  return getModuleIdByName(projectRootPath, path);
}

module.exports = { getModuleId, useIndex };
